//引入http模块
const http = require('http')
const cheerio = require('cheerio')
let url = 'http://www.baidu.com'
//创建请求对象
let req = http.get(url, function (res) {
  let html = ''
  //监听数据
  res.on('data', function (chunk) {
    html += chunk
  })
  //监听结束
  res.on('end', function () {
    //使用cheerio解析html
    let $ = cheerio.load(html)
    //获取标题
    let title = $('title').text()
    console.log(title)
  })
})
//监听错误
req.on('error', function (err) {
  console.log(err)
})
//发送请求
req.end()